package com.veryUsedCar.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.veryUsedCar.entity.CharnnelType;
import com.veryUsedCar.mapper.CharnnelTypeMapper;
import com.veryUsedCar.service.CharnnelTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CharnnelTypeServiceImpl implements CharnnelTypeService {
    @Resource
    private CharnnelTypeMapper charnnelTypeMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public List<CharnnelType> findType() {
        //先判断redis中是否存在
        if(redisTemplate.hasKey("type")){
            String type = (String) redisTemplate.opsForValue().get("type");
            List<CharnnelType> typeList = JSONObject.parseArray(type, CharnnelType.class);
            return typeList;
        }else{
            List<CharnnelType> typeList = charnnelTypeMapper.selectList(null);//mp查询
            String s = JSONObject.toJSONString(typeList);//集合转为字符串
            redisTemplate.opsForValue().set("type",s);
            return typeList;
        }

    }
}
